/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import connexiondb.connexiondb;

import java.io.IOException;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class FCommande extends javax.swing.JFrame {
DefaultTableModel tab= new DefaultTableModel();
String datecommande;
Commande liste = new Commande();
Commande cmd = new Commande();
connexiondb cnx=new connexiondb();
String numcommande; String codefournisseur;
double montant;
double prix;
int quantite;
String codemedicament;
 
    /**
     * Creates new form start
     */
     public FCommande() {
              
        initComponents();
        tab.addColumn("Num Commande");
        tab.addColumn("Code Fournisseur");
        tab.addColumn("Code Medicament");
        tab.addColumn("quantite");
        tab.addColumn("date");
        tab.addColumn("prix");
        tab.addColumn("montant");
        table.setModel(tab);
        grp.add(rdb1);
         grp.add(rdb2);
         init();
         date();
    }
     public void date()
    {
        DateFormat d=new SimpleDateFormat("yyyy-MM-dd");
        Datetxt.setText(d.format(new java.util.Date().getTime()));
    }
     public void afficher() 
    {
Commande listem=new Commande();
        tab.setRowCount(0);
        Vector<Commande> lists = new Vector<Commande>();
        lists = listem.afficher();
        
        for (int i = 0; i < lists.size(); i++) 
        {
            Object[] e = {lists.get(i).getCode(), lists.get(i).getcodeFour(), lists.get(i).getcodemedicament(), lists.get(i).getqtemedicament(), lists.get(i).getdate(), lists.get(i).getprix_achat(), lists.get(i).getmontant()};
            tab.addRow(e);
        }
    }
      public void init()
    {
        try

        {
      
        ResultSet rs=cnx.getDataBase("SELECT codemedicament FROM medicament");
        while (rs.next())
        {
            jComboBox2.addItem(rs.getString(1));
        }
        ResultSet rs1=cnx.getDataBase("SELECT codefournisseur FROM fournisseur");
        while (rs1.next())
        {
            jComboBox1.addItem(rs1.getString(1));
        }
        }
        catch(Exception e)
                {
            e.printStackTrace();
        }
    }
 public void boxat(){
      t.setText("");
                  Datetxt.setText("");
                  jTextField1.setText("");
            txtbox1.setText("");
            txtbox4.setText("");
            jComboBox1.setSelectedItem("");
             jComboBox2.setSelectedItem("");
             searchbox.setText("");
 }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grp = new javax.swing.ButtonGroup();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        rdb1 = new javax.swing.JRadioButton();
        rdb2 = new javax.swing.JRadioButton();
        txtbox1 = new javax.swing.JTextField();
        Datetxt = new javax.swing.JTextField();
        txtbox4 = new javax.swing.JTextField();
        searchbox = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        t = new javax.swing.JTextField();
        nouveau = new javax.swing.JButton();
        menu = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jInternalFrame1.setTitle("Gestion de Commande");
        jInternalFrame1.setVisible(true);
        jInternalFrame1.getContentPane().setLayout(null);

        jButton1.setBackground(new java.awt.Color(255, 255, 255));
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/enr.gif"))); // NOI18N
        jButton1.setText("Ajouter");
        jButton1.setPreferredSize(new java.awt.Dimension(95, 29));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jButton1);
        jButton1.setBounds(30, 430, 130, 31);

        jButton2.setBackground(new java.awt.Color(255, 255, 255));
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/icone_modifier.gif"))); // NOI18N
        jButton2.setText("Modifier");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jButton2);
        jButton2.setBounds(200, 430, 130, 29);

        jButton3.setBackground(new java.awt.Color(255, 255, 255));
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/overview_button_delete.gif"))); // NOI18N
        jButton3.setText("Supprimer");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jButton3);
        jButton3.setBounds(370, 430, 120, 29);

        jButton4.setBackground(new java.awt.Color(255, 255, 255));
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/noloc_env_search_icon.gif"))); // NOI18N
        jButton4.setText("Rechercher");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jButton4);
        jButton4.setBounds(53, 243, 120, 29);

        jButton5.setBackground(new java.awt.Color(255, 255, 255));
        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/images.jpg"))); // NOI18N
        jButton5.setText("Afficher");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jButton5);
        jButton5.setBounds(550, 430, 116, 29);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Numéro de la Commande:");
        jInternalFrame1.getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 50, 148, 35);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Code du Fournisseur:");
        jInternalFrame1.getContentPane().add(jLabel2);
        jLabel2.setBounds(20, 100, 130, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Date de la Commande:");
        jInternalFrame1.getContentPane().add(jLabel3);
        jLabel3.setBounds(449, 95, 170, 14);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Montant de la Commande:");
        jInternalFrame1.getContentPane().add(jLabel4);
        jLabel4.setBounds(449, 181, 170, 14);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(table);

        jScrollPane2.setViewportView(jScrollPane1);

        jInternalFrame1.getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(25, 286, 930, 130);

        rdb1.setForeground(new java.awt.Color(255, 255, 255));
        rdb1.setText("Numéro de la commande");
        jInternalFrame1.getContentPane().add(rdb1);
        rdb1.setBounds(414, 246, 170, 23);

        rdb2.setForeground(new java.awt.Color(255, 255, 255));
        rdb2.setText("Code du Fournisseur");
        jInternalFrame1.getContentPane().add(rdb2);
        rdb2.setBounds(588, 246, 140, 23);
        jInternalFrame1.getContentPane().add(txtbox1);
        txtbox1.setBounds(180, 60, 200, 25);
        jInternalFrame1.getContentPane().add(Datetxt);
        Datetxt.setBounds(770, 90, 200, 25);

        txtbox4.setEnabled(false);
        jInternalFrame1.getContentPane().add(txtbox4);
        txtbox4.setBounds(770, 170, 200, 25);
        jInternalFrame1.getContentPane().add(searchbox);
        searchbox.setBounds(201, 239, 184, 37);

        jLabel5.setFont(new java.awt.Font("Segoe UI Light", 1, 36)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("LA COMMANDE");
        jInternalFrame1.getContentPane().add(jLabel5);
        jLabel5.setBounds(449, 19, 335, 48);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(jComboBox1);
        jComboBox1.setBounds(180, 100, 200, 25);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Code medicament");
        jInternalFrame1.getContentPane().add(jLabel6);
        jLabel6.setBounds(20, 140, 102, 14);

        jInternalFrame1.getContentPane().add(jComboBox2);
        jComboBox2.setBounds(180, 140, 200, 25);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Prix  d'achat");
        jInternalFrame1.getContentPane().add(jLabel7);
        jLabel7.setBounds(20, 190, 110, 14);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Quantite");
        jInternalFrame1.getContentPane().add(jLabel8);
        jLabel8.setBounds(449, 129, 63, 14);
        jInternalFrame1.getContentPane().add(jTextField1);
        jTextField1.setBounds(770, 130, 200, 25);

        t.setToolTipText("");
        jInternalFrame1.getContentPane().add(t);
        t.setBounds(180, 190, 200, 25);

        nouveau.setBackground(new java.awt.Color(255, 255, 255));
        nouveau.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/user-reset.png"))); // NOI18N
        nouveau.setText("Nouveau");
        nouveau.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nouveauActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(nouveau);
        nouveau.setBounds(730, 430, 116, 29);

        menu.setBackground(new java.awt.Color(255, 255, 255));
        menu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/menu.gif"))); // NOI18N
        menu.setText("Menu");
        menu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuActionPerformed(evt);
            }
        });
        jInternalFrame1.getContentPane().add(menu);
        menu.setBounds(900, 430, 116, 29);

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/d8e8c57e4c07ddfdbe387271e2930732_large.jpeg"))); // NOI18N
        jInternalFrame1.getContentPane().add(jLabel9);
        jLabel9.setBounds(-400, 0, 1630, 590);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 1161, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
         
        prix =Double.parseDouble(t.getText());
  datecommande= Datetxt.getText();
  codefournisseur=jComboBox1.getSelectedItem().toString();
   codemedicament=jComboBox2.getSelectedItem().toString();
  numcommande=txtbox1.getText();
  quantite=Integer.valueOf(jTextField1.getText());
     montant=prix*quantite;
  
  
  if(cmd.ajouter(numcommande, codefournisseur,datecommande,montant )==1){
      if(cmd.ajouter1(numcommande, codemedicament,quantite,prix)==1)
            javax.swing.JOptionPane.showMessageDialog(null, "Ajouter avec succes ", "Etat", javax.swing.JOptionPane.INFORMATION_MESSAGE);
  }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       prix =Double.parseDouble(t.getText());
  datecommande= Datetxt.getText();
  codefournisseur=jComboBox1.getSelectedItem().toString();
   codemedicament=jComboBox2.getSelectedItem().toString();
  numcommande=txtbox1.getText();
  quantite=Integer.valueOf(jTextField1.getText());
     montant=prix*quantite;
        if(cmd.modifier(numcommande, codefournisseur,datecommande,montant )==1){
      if(cmd.modifier1(numcommande, codemedicament,quantite,prix)==1)
            javax.swing.JOptionPane.showMessageDialog(null, "Modifier avec succes ", "Etat", javax.swing.JOptionPane.INFORMATION_MESSAGE);
  }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    prix =Double.parseDouble(t.getText());
  datecommande= Datetxt.getText();
  codefournisseur=jComboBox1.getSelectedItem().toString();
   codemedicament=jComboBox2.getSelectedItem().toString();
  numcommande=txtbox1.getText();
  quantite=Integer.valueOf(jTextField1.getText());
     montant=prix*quantite;
        if(cmd.supprimer1(numcommande )==1){
      if(cmd.supprimer(numcommande)==1)
            javax.swing.JOptionPane.showMessageDialog(null, "Supprimer avec succes ", "Etat", javax.swing.JOptionPane.INFORMATION_MESSAGE);
      
  }
        boxat();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
   
      try {
 
       if (rdb1.isSelected()){
     
       ResultSet rs =  cnx.getDataBase("SELECT commande.numcommande ,codefournisseur, codemedicament, qtemedicament,datecommande, prix_achat, montant\n" +
"FROM commande, lignecommande\n" +
"WHERE commande.numcommande = lignecommande.numcommande\n" +
"AND commande.numcommande ='"+searchbox.getText()+"'");
             if (rs.next()){
                  t.setText(rs.getString("prix_achat"));
                  Datetxt.setText(rs.getString("datecommande"));
                  jTextField1.setText(rs.getString("qtemedicament"));
            txtbox1.setText(rs.getString(1));
            txtbox4.setText(rs.getString("montant"));
            jComboBox1.setSelectedItem(rs.getString("codefournisseur"));
             jComboBox2.setSelectedItem(rs.getString("codemedicament"));
             }
       } if (rdb2.isSelected()){
           
        ResultSet rs =  cnx.getDataBase("SELECT commande.numcommande ,codefournisseur, codemedicament, qtemedicament,datecommande, prix_achat, montant\n" +
"FROM commande, lignecommande\n" +
"WHERE commande.numcommande = lignecommande.numcommande\n" +
"AND codefournisseur ='"+searchbox.getText()+"'");
             if (rs.next()){
                  t.setText(rs.getString("prix_achat"));
                  Datetxt.setText(rs.getString("datecommande"));
                  jTextField1.setText(rs.getString("qtemedicament"));
            txtbox1.setText(rs.getString(1));
            txtbox4.setText(rs.getString("montant"));
            jComboBox1.setSelectedItem(rs.getString("codefournisseur"));
             jComboBox2.setSelectedItem(rs.getString("codemedicament"));
             }
             
       }    
       
         
            
      
       }catch (Exception ex) {
           
}        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
 table.setModel(tab);
        afficher();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

        
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void nouveauActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nouveauActionPerformed
boxat();        // TODO add your handling code here:
    }//GEN-LAST:event_nouveauActionPerformed

    private void menuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuActionPerformed
         fenetredemarrage fen = new fenetredemarrage();
            fen.setVisible(true);
            this.setVisible(false);
    }//GEN-LAST:event_menuActionPerformed
 
/**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FCommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FCommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FCommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FCommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FCommande().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Datetxt;
    private javax.swing.ButtonGroup grp;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JButton menu;
    private javax.swing.JButton nouveau;
    private javax.swing.JRadioButton rdb1;
    private javax.swing.JRadioButton rdb2;
    private javax.swing.JTextField searchbox;
    private javax.swing.JTextField t;
    private javax.swing.JTable table;
    private javax.swing.JTextField txtbox1;
    private javax.swing.JTextField txtbox4;
    // End of variables declaration//GEN-END:variables
}
